package com.init.bootframe.config.web;

import com.init.bootframe.config.Filter.MyFilter;
import com.init.bootframe.config.interceptor.InMessageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import javax.servlet.Filter;
import java.io.File;

@Configuration
public class MvcConfiguration extends WebMvcConfigurerAdapter {

    @Autowired //Spring容器
    private ApplicationContext applicationContext;

    @Value("${custom.file.upload}")
    private String uploadPath;

    @Bean
    public InMessageInterceptor inMessageInterceptor(){
        return new InMessageInterceptor();
    }

    @Bean
    Filter myFilter(){
        return new MyFilter();
    }

    @Bean
    public FilterRegistrationBean<MyFilter> filterRegistrationBean1(){
        FilterRegistrationBean<MyFilter> filterRegistrationBean=new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter((MyFilter) myFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        //filterRegistrationBean.setOrder();多个filter的时候order的数值越小 则优先级越高
        return filterRegistrationBean;
    }

    /**
     * 配置静态资源访问
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+ uploadPath + File.separator);
        super.addResourceHandlers(registry);
    }

    /**
     * 配置视图解析器
     * @return
     */
    @Bean(name = "viewResolver")
    public InternalResourceViewResolver resourceViewResolver()
    {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        //设置spring容器
        internalResourceViewResolver.setApplicationContext(this.applicationContext);
        //取消缓存
        internalResourceViewResolver.setCache(false);
        //请求视图文件的前缀地址
        internalResourceViewResolver.setPrefix("/");
        //请求视图文件的后缀
        internalResourceViewResolver.setSuffix(".html");
        return internalResourceViewResolver;
    }

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(inMessageInterceptor()).addPathPatterns("/user/**");
        super.addInterceptors(registry);
    }

//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
//        fastJsonConfig.setSerializerFeatures(
//                //List字段如果为null,输出为[],而非null
//                SerializerFeature.WriteNullListAsEmpty,
//                //是否输出值为null的字段,默认为false
//                SerializerFeature.WriteMapNullValue,
//                //字符串null返回空字符串
//                SerializerFeature.WriteNullStringAsEmpty,
//                //空布尔值返回false
//                SerializerFeature.WriteNullBooleanAsFalse,
//                //结果是否格式化,默认为false
//                SerializerFeature.PrettyFormat);
//        //格式化日期
//        fastJsonConfig.setDateFormat("YYYY-MM-dd HH:mm:ss");
//        converter.setFastJsonConfig(fastJsonConfig);
//        converters.add(converter);
//    }
}
